package com.ckzp.jfinal.weixin.util;

import com.alibaba.fastjson.JSONObject;
import com.ckzp.core.util.XmlUtil;
import com.ckzp.jfinal.weixin.model.WxPay;

import java.util.Map;

/**
 * Created by 51594 on 2021/8/3.
 */
public class WxPayUtil {

    /**
     * @return
     */
    public static WxPayUtil n() {
        return new WxPayUtil();
    }

    /**
     * 同步保存订单
     *
     * @param xmls
     */
    public WxPay saveWxPay(String xmls) {

        JSONObject m = XmlUtil.toJson(xmls);
        WxPay pay = null;
        String transaction_id = m.getString("transaction_id");
        String out_trade_no = m.getString("out_trade_no");
        String result_code = m.getString("result_code");
        if (transaction_id.length() > 0) {
            pay = WxPay.dao.findFirst("select * from wx_pay where out_order_id=?", out_trade_no);
            if (pay == null) {
                new WxPay()
                        .set("out_order_id", out_trade_no)
                        .set("result_code", m.getString("result_code"))
                        .set("return_code", m.getString("return_code"))
                        .set("appid", m.getString("appid"))
                        .set("bank_type", m.getString("bank_type"))
                        .set("cash_fee", m.getString("cash_fee"))
                        .set("device_info", m.getString("device_info"))
                        .set("fee_type", m.getString("fee_type"))
                        .set("is_subscribe", m.getString("is_subscribe"))
                        .set("mch_id", m.getString("mch_id"))
                        .set("nonce_str", m.getString("nonce_str"))
                        .set("out_trade_no", m.getString("out_trade_no"))
                        .set("sign", m.getString("sign"))
                        .set("time_end", m.getString("time_end"))
                        .set("total_fee", m.getIntValue("total_fee"))
                        .set("settlement_total_fee", m.getIntValue("settlement_total_fee"))
                        .set("cash_fee", m.getIntValue("cash_fee"))
                        .set("trade_type", m.getString("trade_type"))
                        .set("err_code", m.getString("err_code"))
                        .set("err_code_des", m.getString("err_code_des"))
                        .set("openid", m.getString("openid"))
                        .set("attach", m.getString("attach"))
                        .set("transaction_id", transaction_id)
                        .save();
            } else {
                pay.set("result_code", m.getString("result_code"))
                        .set("return_code", m.getString("return_code"))
                        .set("appid", m.getString("appid"))
                        .set("bank_type", m.getString("bank_type"))
                        .set("cash_fee", m.getString("cash_fee"))
                        .set("device_info", m.getString("device_info"))
                        .set("fee_type", m.getString("fee_type"))
                        .set("is_subscribe", m.getString("is_subscribe"))
                        .set("mch_id", m.getString("mch_id"))
                        .set("nonce_str", m.getString("nonce_str"))
                        .set("out_trade_no", m.getString("out_trade_no"))
                        .set("sign", m.getString("sign"))
                        .set("time_end", m.getString("time_end"))
                        .set("total_fee", m.getIntValue("total_fee"))
                        .set("settlement_total_fee", m.getIntValue("settlement_total_fee"))
                        .set("cash_fee", m.getIntValue("cash_fee"))
                        .set("trade_type", m.getString("trade_type"))
                        .set("err_code", m.getString("err_code"))
                        .set("err_code_des", m.getString("err_code_des"))
                        .set("openid", m.getString("openid"))
                        .set("attach", m.getString("attach"))
                        .set("transaction_id", m.getString("transaction_id"))
                        .update();
            }
        }
        return pay;
    }

    /**
     * 取得支付成功的订单支付信息
     *
     * @param orderid
     * @return
     */
    public WxPay getSuccWxPay(String orderid) {
        return WxPay.dao.findFirst("select * from wx_pay where out_order_id=? and result_code=?", orderid, "SUCCESS");
    }
}
